﻿using Model;
using System.ComponentModel;

namespace DAL
{
    public class RoomTypeDAL
    {

        public RoomTypeDAL(MyContext myContext)
        {
            MyContext = myContext;
        }

        public MyContext MyContext { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="roomType"></param>
        /// <returns></returns>
        public int Add(RoomType roomType)
        {
            try
            {
                MyContext.RoomTypes.Add(roomType);
                return MyContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            try
            {
               var del = MyContext.RoomTypes.FirstOrDefault(x=>x.Id == id);
               if(del != null)
               {
                   MyContext.RoomTypes.Remove(del);
               }
               return MyContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="roomType"></param>
        /// <returns></returns>
        public int Update(RoomType roomType)
        {
            try
            {
                var upd = MyContext.RoomTypes.Find(roomType.Id);
                if (upd != null)
                {
                    upd.Pic = roomType.Pic;
                    upd.Price = roomType.Price;
                    upd.Isyes = roomType.Isyes;
                    upd.Time = roomType.Time;
                    upd.Name = roomType.Name;
                    upd.Cm = roomType.Cm;
                    upd.Ruzhu = roomType.Ruzhu;
                    upd.ZhuangTai = roomType.ZhuangTai;
                }
                return MyContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<RoomType> GetShow()
        {
            try
            {
                return MyContext.RoomTypes.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反添
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoomType GetType(int id)
        {
            try
            {
                return MyContext.RoomTypes.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Count"></param>
        /// <param name="Sum"></param>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        public List<RoomType> Show(string? name,out int Count,out int Sum,int Index=1,int Size = 2)
        {
            try
            {
                var list = MyContext.RoomTypes.ToList();
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x=> x.Name.Contains(name)).ToList();
                }
                Count = list.Count();
                Sum = (int)Math.Ceiling(Count * 1.0 / Size);
                list = list.OrderBy(x=>x.Id).Skip((Index-1)*Size).Take(Size).ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
